/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.dataoke.shoppingguide.app773026.R;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class GlideUtils {

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImgFit(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().override(600).fitCenter()).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }
    public static void loadImgDetail(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).override(700)
                .fitCenter()
                .into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {

                    }

                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        imageView.setImageDrawable(resource);
                    }

                    @Override
                    protected void onResourceCleared(@Nullable Drawable placeholder) {

                    }

                });
    }
    public static void loadImgPopupFit(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().override(400).fitCenter().transform(new RoundedCorners(10))).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }
    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImgCrop(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().override(200).centerCrop().transform(new RoundedCorners(20))).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImgCrop(Fragment context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().override(200).centerCrop().transform(new RoundedCorners(20))).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImgCrop(Fragment context, String url, ImageView imageView, int width, int height) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().override(width, height).centerCrop()).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadAvHeadImg(Fragment context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .apply(new RequestOptions().centerCrop().transform(new RoundedCorners(20)))
                .error(R.mipmap.av_error)
                .placeholder(R.mipmap.av_error)
                .into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadAvHeadImg(Activity context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .apply(new RequestOptions().centerCrop().transform(new RoundedCorners(20)))
                .error(R.mipmap.av_error)
                .placeholder(R.mipmap.av_error)
                .into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {

                    }

                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        imageView.setImageDrawable(resource);
                    }

                    @Override
                    protected void onResourceCleared(@Nullable Drawable placeholder) {

                    }

                });
    }

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadCategoryImg(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().centerCrop().transform(new RoundedCorners(20))).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }
    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadNavCrop(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).apply(new RequestOptions().override(200).centerCrop().transform(new RoundedCorners(20))).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }

    public static void loadTileShortCrop(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).apply(new RequestOptions().centerCrop().transform(new RoundedCorners(20))).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }

    /**
     * 加载图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadWaimaiImgCrop(Context context, String url, ImageView imageView) {
        if (context == null || imageView == null) {
            return;
        }
        Glide.with(context).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().fitCenter()).into(new  CustomViewTarget<ImageView, Drawable>(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

        });
    }
}
